package com.zdlog.wenjuan.controller.web;

import com.zdlog.wenjuan.constant.CookieConstant;
import com.zdlog.wenjuan.constant.RedisConstant;
import com.zdlog.wenjuan.dataobject.UserInfo;
import com.zdlog.wenjuan.service.FormService;
import com.zdlog.wenjuan.service.UserService;
import com.zdlog.wenjuan.util.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.controller.web
 * @author: Kuaile
 * @create: 2018-02-25 17:11
 * 问卷审核Controller
 **/


@Controller
@RequestMapping("/audit")
@ResponseBody
@Slf4j
public class AuditController {

    @Autowired
    private FormService formService;


    @Autowired
    private UserService service;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/list")
    public ModelAndView list(HttpServletRequest request,
                             Map<String, Object> map,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        //去redis里查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
        UserInfo userInfo = service.findUserId(tokenValue);
        PageRequest pageRequest = new PageRequest(page - 1, size);
        map.put("currentPage", page);
        map.put("userId", tokenValue);
        map.put("size", size);
        map.put("icon", userInfo.getIcon());
        map.put("formList", formService.list(tokenValue, pageRequest));
        return new ModelAndView("audit/list");
    }
}
